﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookingHotel.ClasseDiDominio;

namespace BookingHotel.Collezioni
{
    class BookingCollection
    {
        private List<Booking> _bookingList = new List<Booking>();

        public void AddPrenotations(Booking newPrenotations)
        {
            if (_bookingList.Find(prenotations => prenotations.Id == newPrenotations.Id) == null)
                _bookingList.Add(newPrenotations);
            else
                throw new Exception("Duplicate id for prenotations.");
        }

        public void AddPrenotations(Client client, string typeRoom, int numberOfRoom, decimal price, DateTime startBooking, DateTime endBooking, int bookingId)
        {
            int idOfNewClient = _bookingList.Count;
            Booking newClient = new Booking(client, typeRoom, numberOfRoom, price, startBooking, endBooking, idOfNewClient);
            AddPrenotations(newClient);
        }

        public Booking GetBookingById(int clientId)
        {
            return _bookingList.Find(c => c.Id == clientId);
        }

        public String ToString()
        {
            String allBookingToString = "";
            foreach (Booking currentBooking in _bookingList)
                allBookingToString += currentBooking.ToString();
            return allBookingToString;
        }

        public int GetOccupiedRoom(string typeRoom, DateTime startPeriod, DateTime endPeriod)
        {
            List<Booking> listOfBookingOfthisTypeRoomInThatPeriod=
                _bookingList.FindAll(b =>   startPeriod >= b.StartBooking && startPeriod <= b.StartBooking ||
                                            endPeriod >= b.StartBooking && endPeriod <= b.StartBooking ||
                                            startPeriod <= b.StartBooking && endPeriod>=b.EndBooking    );

            return listOfBookingOfthisTypeRoomInThatPeriod.Min(b => b.NumberOfRoom);
        }
    }
}
